package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Auther: xiang
 * @Date: 2019/8/18 20:52
 * @Description:http://api.leyou.com/api/item/spu/page?key=&saleable=true&page=1&rows=5
 */
@RestController
@RequestMapping
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询spu
     *
     * @param page     当前页
     * @param rows     每页展示多少行
     * @param key      查询的关键字
     * @param saleable 上架或下架的
     * @return当前页的商品数据
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>> pageSpu(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                      @RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                                      @RequestParam(value = "key", required = false) String key,
                                                      @RequestParam(value = "saleable", required = false) Boolean saleable) {
        return ResponseEntity.ok(goodsService.querySpuByPage(page, rows, saleable, key));


    }

    @PostMapping("/goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        goodsService.saveGoods(spuDTO);
        return new ResponseEntity<>(HttpStatus.CREATED);

    }

    @PutMapping("/spu/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id") Long id, @RequestParam("saleable") Boolean saleable) {
        goodsService.updateSaleable(id, saleable);
        return new ResponseEntity<>(HttpStatus.OK);

    }
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetail(@RequestParam("id")Long id ){
       SpuDetailDTO spuDetailDTO= goodsService.querySpuDetail(id);
return ResponseEntity.ok(spuDetailDTO);

    }
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySku(@RequestParam("id")Long id ){
        List<SkuDTO> spuDetailDTO= goodsService.querySku(id);
        return ResponseEntity.ok(spuDetailDTO);

    }
    @PutMapping("goods")
  public  ResponseEntity<Void>   updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();



  }
/**
 * 通过spuid查询spu
 */
@GetMapping("spu/{id}")
public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){

    return ResponseEntity.ok(goodsService.querySpuById(id));

}

    /**
     * 通过skuid集合来查询sku
     * @return
     */
    @GetMapping("/sku/list")
public ResponseEntity<List<SkuDTO>> querySkuByList(@RequestParam("ids") List<Long> ids){
        List<SkuDTO>  skuDTOList=   goodsService.querySkuList(ids);
     return ResponseEntity.ok(skuDTOList);
    }

    /**
     * 减库存
     * @param cartMap 商品id及数量的map
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long, Integer> cartMap){
        goodsService.minusStock(cartMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }



}